package JZOffer;

import java.util.Stack;

/**
 * @author Lw中
 * @date 2021/8/19 21:26
 */
public class Jz18 {

    // Jz18.删除链表的节点

     public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
     }

    public ListNode deleteNode(ListNode head, int val) {
        // 使用双指针
        if (head.val == val) return head.next;
        ListNode temp = head;
        ListNode temp1 = head.next;
//        // 遍历循环到与val值相同的节点后跳出循环
//        while (temp1 != null && temp1.val != val) {
//            temp = temp1;
//            temp1 = temp1.next;
//        }
//        // 跳出循环之后若与val相同的节点的下一个节点不为空便将temp的next指向temp1的next
//        if (temp1 != null) temp.next = temp1.next;
        while (temp1 != null) {
            if (temp1.val == val) {
                temp.next = temp1.next;
                break;
            }
            temp = temp1;
            temp1 = temp1.next;
        }
        return head;
    }

}
